#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n;
char op, s[maxn];
const int N = 2e4+7;
int trie[N][26], cnt[N], idx;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  cin>>n;
  while(n--) {
    cin>>op>>s;
    int p = 0;
    if(op=='I') {
      for(int i=0; s[i]; ++i) {
        int ch = s[i]-'a';
        if(!trie[p][ch]) trie[p][ch] = ++idx;
        p = trie[p][ch];
      }
      cnt[p]++;
    }
    else if(op=='Q') {
      bool ok = true;
      for(int i=0; s[i]; ++i) {
        int ch = s[i]-'a';
        if(!trie[p][ch])  {
          ok = false;
          break;
        }
        p = trie[p][ch];
      }
      cout<<(ok?cnt[p]:0)<<'\n';
    }
    else assert(false);
  }
  return 0;
}

